package com.wtsw.pulsosocial.thread;

import com.wtsw.pulsosocial.R;
import com.wtsw.pulsosocial.controller.GestorEventos;
import com.wtsw.pulsosocial.model.structure.Evento;
import com.wtsw.pulsosocial.view.event.FormEvento;

import android.app.Dialog;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class EventoThread extends AsyncTask<Void, Void, Void> {	
	FormEvento fe;
	Evento evento;

	public EventoThread(FormEvento formEvento, Evento evento) {
		// TODO Auto-generated constructor stub		
		this.fe = formEvento;
		this.evento = evento;
	}

	@Override
    protected void onPreExecute() {        
        fe.getpDialog().setMessage("Enviando Pulso... Espere por favor");
        fe.getpDialog().setIndeterminate(false);
        fe.getpDialog().setCancelable(false);
        fe.getpDialog().show();
    }

	@Override
	protected Void doInBackground(Void... params) {        
		hardcodeo_serio();		
		return null; 
	}
	
	protected void hardcodeo_serio() {
		// TODO Auto-generated method stub
		try {
		    Thread.sleep(1000);         
		} catch (InterruptedException e) {
		     e.printStackTrace();
		}		
		GestorEventos.populate();    	
        GestorEventos.addEvent(evento);
        
	}
	
    protected void onPostExecute(Void result) {
    	//ctx.startActivity(new Intent(ctx, MainMenu.class));
    	if(fe.getpDialog() != null)
    		fe.getpDialog().dismiss();    	
    	// custom dialog
		fe.setPulseDialog(new Dialog(fe));
		fe.getPulseDialog().setContentView(R.layout.dialog_pulso);
		fe.getPulseDialog().setTitle("Has Pulseado!");	     
		// set the custom dialog components - text, image and button
		TextView text = (TextView) fe.getPulseDialog().findViewById(R.id.text);
		text.setText("Android custom dialog example!");
		ImageView image = (ImageView) fe.getPulseDialog().findViewById(R.id.image);
		//image.setImageResource(R.drawable.ic_launcher);
		image.setImageBitmap(evento.getSubCategoria().getPadre().getIcono());
		
		Button dialogButton = (Button) fe.getPulseDialog().findViewById(R.id.dialogButtonOK);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				fe.getPulseDialog().dismiss();
				fe.getMainFragmentActivity().finish();
			}
		});	     
		fe.getPulseDialog().show();    	
    }
}